<?php
session_start();
require_once 'core/PluginLoader.php';
require_once 'core/PluginModel.php';

$pluginDir = __DIR__ . '/plugins';
$loader = new PluginLoader($pluginDir);
$pluginModel = new PluginModel();

$pluginName = $_GET['name'] ?? '';
if (empty($pluginName)) {
    header('Location: index.php');
    exit;
}

// 获取插件信息
$pluginInfo = $loader->getPluginInfo($pluginName);
$dbInfo = $pluginModel->getPluginByName($pluginName);

if (!$pluginInfo || !$dbInfo) {
    header('Location: index.php');
    exit;
}

// 合并信息
$plugin = array_merge($pluginInfo, $dbInfo);

// 增加浏览量
$pluginModel->incrementViewCount($pluginName);

// 获取插件分类
$categories = $pluginModel->getPluginCategories($pluginName);

// 处理点赞请求
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'like') {
    $likeKey = 'liked_' . $pluginName;
    
    if (!isset($_SESSION[$likeKey])) {
        $pluginModel->incrementLikeCount($pluginName);
        $_SESSION[$likeKey] = true;
        echo json_encode(['success' => true, 'message' => '点赞成功']);
    } else {
        echo json_encode(['success' => false, 'message' => '您已经点过赞了']);
    }
    exit;
}

// 渲染插件内容
ob_start();
$pluginContent = $loader->render($pluginName, $plugin);
if (empty($pluginContent)) {
    echo '<div class="text-center py-8 text-gray-500">插件渲染失败</div>';
} else {
    echo $pluginContent;
}
$pluginContent = ob_get_clean();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($plugin['title']); ?> - 幻影工具网</title>
    <meta name="description" content="<?php echo htmlspecialchars($plugin['description']); ?>">
    <meta name="keywords" content="<?php echo htmlspecialchars($plugin['title']); ?>,在线工具,<?php echo htmlspecialchars($plugin['description']); ?>">
    <meta property="og:title" content="<?php echo htmlspecialchars($plugin['title']); ?> - 幻影工具网">
    <meta property="og:description" content="<?php echo htmlspecialchars($plugin['description']); ?>">
    <meta property="og:type" content="website">
    <meta property="og:url" content="<?php echo 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>">
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#6366F1'
                    }
                }
            }
        }
    </script>
</head>
<body class="bg-gradient-to-br from-blue-50 to-indigo-50 min-h-screen flex flex-col">
    <!-- 顶部导航 -->
    <nav class="bg-white/80 backdrop-blur-sm shadow-sm border-b border-blue-100 sticky top-0 z-50">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between items-center h-16">
                <div class="flex items-center">
                    <a href="/" class="text-2xl font-bold bg-gradient-to-r from-blue-600 to-indigo-600 bg-clip-text text-transparent">
                        幻影工具网
                    </a>
                </div>
                <div class="flex items-center space-x-4">
                    <a href="/" class="text-gray-600 hover:text-blue-600 transition-colors">返回首页</a>
                    <a href="/admin" class="text-gray-600 hover:text-blue-600 transition-colors">后台管理</a>
                </div>
            </div>
        </div>
    </nav>

    <div class="flex-1">
        <!-- 插件头部信息 -->
        <div class="bg-white/60 backdrop-blur-sm border-b border-blue-100">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
                <div class="flex items-start justify-between">
                    <div class="flex items-start space-x-6">
                        <div class="w-16 h-16 rounded-2xl bg-gradient-to-br from-blue-500 to-indigo-600 flex items-center justify-center text-white font-bold text-2xl shadow-lg">
                            <?php echo strtoupper(substr($plugin['title'], 0, 1)); ?>
                        </div>
                        <div>
                            <h1 class="text-3xl font-bold text-gray-800 mb-2"><?php echo htmlspecialchars($plugin['title']); ?></h1>
                            <p class="text-gray-600 text-lg mb-3"><?php echo htmlspecialchars($plugin['description']); ?></p>
                            <div class="flex items-center space-x-6 text-sm text-gray-500">
                                <span class="flex items-center">
                                    <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>
                                    </svg>
                                    <?php echo number_format($plugin['view_count']); ?> 次浏览
                                </span>
                                <span class="flex items-center">
                                    <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"></path>
                                    </svg>
                                    <span id="likeCount"><?php echo number_format($plugin['like_count']); ?></span> 个赞
                                </span>
                                <?php if (!empty($categories)): ?>
                                <span class="flex items-center">
                                    <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"></path>
                                    </svg>
                                    <?php echo htmlspecialchars(implode(', ', array_column($categories, 'name'))); ?>
                                </span>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                    <div class="flex items-center space-x-3">
                        <button id="likeBtn" onclick="likePlugin()" class="flex items-center px-4 py-2 bg-white border border-blue-200 rounded-lg hover:bg-blue-50 hover:border-blue-300 transition-colors">
                            <svg class="w-4 h-4 mr-2 text-red-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"></path>
                            </svg>
                            点赞
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 插件内容 -->
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
            <div class="bg-white/80 backdrop-blur-sm rounded-2xl shadow-sm border border-blue-100 p-8">
                <?php echo $pluginContent; ?>
            </div>
        </div>
    </div>

    <?php include 'foot.php'; ?>

    <script>
    function likePlugin() {
        const btn = document.getElementById('likeBtn');
        const countSpan = document.getElementById('likeCount');
        
        // 防止重复点击
        btn.disabled = true;
        btn.innerHTML = '<svg class="w-4 h-4 mr-2 animate-spin" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path></svg>处理中...';
        
        fetch('plugin.php?name=<?php echo urlencode($pluginName); ?>', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: 'action=like'
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                // 更新点赞数
                const currentCount = parseInt(countSpan.textContent.replace(/,/g, ''));
                countSpan.textContent = (currentCount + 1).toLocaleString();
                
                // 更新按钮状态
                btn.innerHTML = '<svg class="w-4 h-4 mr-2 text-red-500" fill="currentColor" viewBox="0 0 24 24"><path fill-rule="evenodd" d="M3.172 5.172a4 4 0 015.656 0L10 6.343l1.172-1.171a4 4 0 115.656 5.656L10 17.657l-6.828-6.829a4 4 0 010-5.656z" clip-rule="evenodd"></path></svg>已点赞';
                btn.classList.add('bg-red-50', 'border-red-300', 'text-red-600');
                btn.classList.remove('hover:bg-blue-50', 'hover:border-blue-300');
            } else {
                alert(data.message);
                btn.disabled = false;
                btn.innerHTML = '<svg class="w-4 h-4 mr-2 text-red-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"></path></svg>点赞';
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('点赞失败，请稍后重试');
            btn.disabled = false;
            btn.innerHTML = '<svg class="w-4 h-4 mr-2 text-red-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"></path></svg>点赞';
        });
    }
    </script>
</body>
</html>
